$(function () {
    //高级搜索
    queryAjaxGroomPlaceList(1);
    $('body').on('click','#submitFrom,#querySubmit',function () {
        //点击搜索查询从第一页开始查询
        queryAjaxGroomPlaceList(1);
    });
    deleteGroomPlace();
    //批量冻结
    $('body').on("click", "#unlockBatch", function () {
        var obj = $("input[name='groomPlaceId']:checked");
        var groomPlaceIds = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(groomPlaceIds)) {
            updateGroomPlaceStatusByIds(groomPlaceIds, obj, 2);
        }
    });
    //批量恢复
    $("body").on("click", "#historyBatch", function () {
        var obj = $("input[name='groomPlaceId']:checked");
        var groomPlaceIds = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(groomPlaceIds)) {
            updateGroomPlaceStatusByIds(groomPlaceIds, obj, 1);
        }
    });
    //批量删除
    $("body").on("click", "#deleteBatch", function () {
        var obj = $("input[name='groomPlaceId']:checked");
        var groomPlaceIds = obtainIds(obj);//获取勾选ids
            if (!$.isEmptyObject(groomPlaceIds)) {
                icModale(2, "删除后不可恢复，仍然确认删除么？", 3, function () {
                updateGroomPlaceStatusByIds(groomPlaceIds, obj, 3);
                });
            }
    });
    //单个冻结
    $('body').on('click', '.unlockAndHistory', function () {
        var groomPlaceIds = $(this).data('id');
        var status = $(this).data('status');
        var obj = $(this);
        if (status == 1) {
            updateGroomPlaceStatusByIds(groomPlaceIds, obj, 2);
        }
        if (status == 2) {
            updateGroomPlaceStatusByIds(groomPlaceIds, obj, 1);
        }
    });
    //判断勾选ID是否为空
    lookGroomPlace();
});
function obtainIds(obj) {
    if (obj.size() == 0) {
        icModale(0, "请选择要批量操作的数据");
        return null;
    } else {
        var groomPlaceIds = "";
        obj.each(function (index, em) {
            groomPlaceIds += em.value + ",";
        })
        groomPlaceIds = groomPlaceIds.substr(0, groomPlaceIds.length - 1);
        return groomPlaceIds;
    }
}
/**
 * 批量封禁，批量恢复，批量删除
 * @param userIds
 * @param obj
 * @param isavalible
 */
function updateGroomPlaceStatusByIds(groomPlaceIds, obj, status) {
    $.ajax({
        url: baseloaction + "/admin/op/groom/updateGroomPlaceStatusByIds",
        data: {"groomPlaceIds": groomPlaceIds, "status": status},
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                if (2 == status) {
                    obj.parents('tr').find('.status').html('');
                    obj.parents('tr').find('.status').append('<span class="label label-danger">冻结</span>');
                    obj.parents('tr').find(".unlockAndHistory").html("");
                    obj.parents('tr').find(".unlockAndHistory").data('status', '2');
                    obj.parents('tr').find(".unlockAndHistory").append('<i class="fa fa-history"></i>');
                    obj.parents('tr').find(".unlockAndHistory").attr("data-original-title","恢复")
                }
                if (1 == status) {
                    obj.parents('tr').find('.status').html('');
                    obj.parents('tr').find('.status').append('<span class="label label-info">正常</span>');
                    obj.parents('tr').find(".unlockAndHistory").html("");
                    obj.parents('tr').find(".unlockAndHistory").data('status', '1');
                    obj.parents('tr').find(".unlockAndHistory").append('<i class="fa fa-unlock-alt"></i>');
                    obj.parents('tr').find(".unlockAndHistory").attr("data-original-title","冻结")
                }
                if(3 == status ){
                    icModale(0, result.message);
                }
            } else {
                icModale(0, result.message);
            }
        },
        error: function (error) {
            icModale(0, "系统繁忙,请稍后重试");
        }
    })
}


/**
 * 清空推荐内容
 */
function deleteGroomPlace() {
    $('body').on('click','.deleteGroomPlace',function () {
        var dataType =$(this).data('type');
        var placeKey =$(this).data('key');
        icModale(2, '确认删除选中的推荐吗?', '', function () {
        $.ajax({
            url:baseloaction+"/admin/op/groom/deleteGroomData",
            type:"post",
            data:{"dataType":dataType,"placeKey":placeKey},
            dataType:"json",
            success:function (result) {
                if(result.success==true){
                    icModale(0,result.message);
                }else{
                    icModale(0, result.message);
                }l
            },
            error:function (error) {
                icModale(0, "系统繁忙，请稍后再试");
            }
        });
        })
    });
}


/**
 * 获取推荐列表页面
 * @param pageNo 当前页码
 */
function queryAjaxGroomPlaceList(pageNo) {
    var params = $("#search-form").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/op/groom/queryAjaxGroomPlaceListPage',//请求URL
        contextId: 'groomPlace-list-context',//响应内容页面设置内容ID
        params: params,//请求参数
        pageNo: pageNo,//页码
        pageCall: queryAjaxGroomPlaceList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId: 'select-all-groomPlace', //全选框ID
        callback: function () {//数据加载完成回调
            $("#totalNum").html("（共"+total+"个推荐位置）");
        }
    };
    ajaxGetListData(obj)
}


function lookGroomPlace() {
    $('body').on('click',"#lookGroomPlace",function () {
        var groomPlaceId=$(this).data('id');
        var index = parent.layer.open({
            type: 2,
            shadeClose: false,
            shade: [0.3, '#000000'],
            title: '<strong>查看推荐位置</strong>',
            area: winArea,
            content: baseloaction + "/admin/op/groom/opGroomPlaceInfoById?id="+groomPlaceId,
            btn: ['关闭'],
            yes: function (index, layero) {
                parent.layer.close(index);
            }
        });
    })    
}
